package http

import (
	"gitee.com/captials-team/ubdframe/src/domain/configstc"
	"gitee.com/captials-team/ubdframe/src/domain/dto/respdata"
	"gitee.com/captials-team/ubdframe/src/domain/services"
	"gitee.com/captials-team/ubdframe/src/domain/vo"
	"gitee.com/captials-team/ubdframe/src/pkg/gin_http"
	v1log "gitee.com/captials-team/ubdframe/src/pkg/logs"
	"github.com/gin-gonic/gin"
)

type RssController struct {
	l     v1log.ILog
	cache services.RssCache
	conf  *configstc.RssAppConfig

	gin_http.ResponseController
}

func NewRssController(l v1log.ILog, conf *configstc.RssAppConfig, cache services.RssCache) *RssController {
	ctr := &RssController{
		l:     l,
		cache: cache,
		conf:  conf,
	}

	return ctr
}

// RssCacheItems godoc
// @Summary      RSS文章列表(缓存)
// @Description  rss内容列表(缓存)
// @Tags         rss
// @Produce      json
// @success 200  {object} respdata.ResponseData{data=[]vo.RssItems} "授权成功"
// @success 500 {object} respdata.ResponseData{} "授权失败"
// @Router       /rss/items [post]
func (ctr *RssController) RssCacheItems(ctx *gin.Context) {
	var list []*vo.RssItems

	for _, v := range ctr.conf.RssConfig {
		body, err := ctr.cache.Get(v.Name)
		if err != nil {
			ctr.l.Error("cache get err %s,%s", err, v.Name)
			ctr.Response(ctx, respdata.CError)
			return
		}
		for _, item := range body.Channel.Items {
			item.Author = v.Name //修改作者
			list = append(list, item)
		}
	}

	ctr.Response(ctx, respdata.CSuccess.MData(list))
}
